package org.example.demo1startjava17.demo;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class Suanfa001 {
    /**
     * 输入：n = 5 ,k = 2 ,data = [1, 2, 3, 3, 2]
     * 输出：9
     * 输入：n = 6 ,k = 3 ,data = [4, 1, 5, 2, 1, 3]
     * 输出：9
     * 输入：n = 4 ,k = 1 ,data = [3, 2, 4, 1]
     * 输出：10
     * 18 7 [2,20,15,21,21,24,10,25,14,14,21,9,11,25,2,23,12,23]
     * * 89
     *
     * @param args
     */
    public static void main(String[] args) {
//        System.out.println(solution003R1("1294512.12412").equals("1,294,512.12412"));
        System.out.println(solution003R1("0000123456789.99").equals("123,456,789.99"));
        System.out.println(solution003R1("987654321").equals("987,654,321"));
    }

    public static int solution001R1(int[] cards) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < cards.length; i++) {
            if (!set.add(cards[i])) {
                set.remove(cards[i]);
            }
        }
        return new ArrayList<>(set).get(0);
    }

    /**
     * 取出唯一单个的数字，其他非单个数字都是成对出现的
     * 异或运算
     * 相同为0，不同为1
     * 交换律和结合律
     * 0 ^ a = a
     * a ^ a = 0
     * a ^ b ^ a = a ^ a ^ b = 0 ^ b = b
     * a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c = (a ^ c) ^ b = b ^ (a ^ c)
     * <p>
     * int[] nums = {1, 1, 3, 3, 5, 5, 7, 7, 9};
     *
     * @param cards
     * @return
     */
    public static int solution001R2(int[] cards) {
        int result = 0;
        for (int card : cards) {
            result ^= card;
        }
        return result;
    }

    /**
     * 小R正在计划一次从地点A到地点B的徒步旅行，总路程需要 N 天。为了在旅途中保持充足的能量，小R每天必须消耗1份食物。幸运的是，小R在路途中每天都会经过一个补给站，可以先购买完食物后再消耗今天的1份食物。然而，每个补给站的食物每份的价格可能不同，并且小R在购买完食物后最多只能同时携带 K 份食物。
     * 现在，小R希望在保证每天食物消耗的前提下，以最小的花费完成这次徒步旅行。你能帮助小R计算出最低的花费是多少吗？
     *
     * @param n    总路程需要的天数
     * @param k    小R最多能同时携带食物的份数
     * @param data 第i天补给站每份食物的价格
     * @return 输入：n = 5 ,k = 2 ,data = [1, 2, 3, 3, 2]
     * 输出：9
     * 输入：n = 6 ,k = 3 ,data = [4, 1, 5, 2, 1, 3]
     * 输出：9
     * 输入：n = 4 ,k = 1 ,data = [3, 2, 4, 1]
     * 输出：10
     * 18 7 [2,20,15,21,21,24,10,25,14,14,21,9,11,25,2,23,12,23]
     * 89
     * 20 10 [25,13,18,2,22,18,12,2,5,18,13,25,20,22,13,14,12,10,21,18]
     * 104
     */
    public static int solution002R1(int n, int k, int[] data) {
        int remainCount = 0;
        int buyCount = 0;
        int buyMoney = 0;
        for (int i = 0; i < data.length; i++) {
            buyCount = solution002R1GetBuyCount(k, data, i, remainCount);
            buyMoney += buyCount * data[i];
            remainCount = remainCount + buyCount - 1;
        }
        return buyMoney;
    }

    /**
     * 需要买的食物数量
     *
     * @param k
     * @param data
     * @param day
     * @param remainCount
     * @return
     */
    private static int solution002R1GetBuyCount(int k, int[] data, int day, int remainCount) {
        int maxCount = 0;
        if (remainCount == 0) {
            maxCount = solution002R1GetBuyCount(data, day, Math.min(day + k - 1, data.length - 1));
        } else {
            boolean buy = solution002R1HasRemainWhetherBuy(data, day, remainCount);
            if (!buy) {
                return 0;
            }
            maxCount = solution002R1GetBuyCount(data, day, Math.min(day + k - 1, data.length - 1));
        }
        return Math.min(maxCount - remainCount, k - remainCount);
    }

    /**
     * 需要购买食物的数量
     *
     * @param data
     * @param startIndex
     * @param endIndex
     * @return
     */
    private static int solution002R1GetBuyCount(int[] data, int startIndex, int endIndex) {
        int index = 1;
        if (startIndex == endIndex) {
            return index;
        }
        int startData = data[startIndex];
        for (int i = startIndex + 1; i <= endIndex; i++) {
            if (data[i] <= startData) {
                return index;
            }
            index++;
        }
        return index;
    }

    /**
     * 当天有余粮的情况下，是否需要购买食物
     *
     * @param data
     * @param day
     * @param remainCount
     * @return
     */
    private static boolean solution002R1HasRemainWhetherBuy(int[] data, int day, int remainCount) {
        if (day + remainCount >= data.length) {
            return false;
        }
        int startData = data[day];
        for (int i = day + 1; i <= Math.min(day + remainCount, data.length - 1); i++) {
            if (data[i] < startData) {
                return false;
            }
        }
        return true;
    }

    /**
     * 小M在工作时遇到了一个问题，他需要将用户输入的不带千分位逗号的数字字符串转换为带千分位逗号的格式，并且保留小数部分。小M还发现，有时候输入的数字字符串前面会有无用的 0，这些也需要精简掉。请你帮助小M编写程序，完成这个任务。
     * <p>
     * System.out.println(solution("1294512.12412").equals("1,294,512.12412"));
     * System.out.println(solution("0000123456789.99").equals("123,456,789.99"));
     * System.out.println(solution("987654321").equals("987,654,321"));
     *
     * @param str
     * @return
     */
    public static String solution003R1(String str) {
        String[] split = solution003R1ReplaceLeft0(str).split("\\.");
        String s1 = split[0];
        char[] charArray = s1.toCharArray();
        int mod = charArray.length % 3;

        List<Character> list = new ArrayList<>();
        for (int i = 0; i < charArray.length; i++) {
            if (i != 0 && i % 3 == mod) {
                list.add(',');
            }
            list.add(charArray[i]);
        }
        StringBuilder stringBuilder = new StringBuilder();
        list.forEach(stringBuilder::append);
        if (split.length > 1) {
         stringBuilder.append(".")
                 .append(split[1]);
        }
        return stringBuilder.toString();
    }
    public static String solution003R1ReplaceLeft0(String str) {
        if (str.startsWith("0")) {
            str = str.substring(1);
            return solution003R1ReplaceLeft0(str);
        } else {
            return str;
        }
    }


}
